package com.example.child_protection.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.child_protection.dao.SystemVaccineDao;
import com.example.child_protection.dto.info.SystemVaccineDTO;
import com.example.child_protection.dto.request.SimpleIdRequestDTO;
import com.example.child_protection.dto.request.find.SystemVaccineRequestDTO;
import com.example.child_protection.dto.response.SystemVaccineListResponseDTO;
import com.example.child_protection.dto.response.SystemVaccineResponseDTO;
import com.example.child_protection.enums.ServiceActionEnums;
import com.example.child_protection.model.SystemVaccineModel;
import com.example.child_protection.service.SystemVaccineQueryService;
import com.example.child_protection.service.base.MyBaseService;
import com.example.child_protection.service.base.MyServiceExecutor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 疫苗查询service实现层
 *
 * @author Biz
 * @date 2018-11-2
 */
@Component
public class SystemVaccineQueryServiceImpl extends MyBaseService implements SystemVaccineQueryService {

    @Autowired
    public SystemVaccineDao systemVaccineDao;

    /**
     * id查询
     *
     * @param requestDTO
     * @return
     */
    public SystemVaccineResponseDTO findId(SimpleIdRequestDTO requestDTO) {
        return this.service(ServiceActionEnums.SYSTEM_VACCINE_FIND_ID, requestDTO, SystemVaccineResponseDTO.class, new MyServiceExecutor<SimpleIdRequestDTO, SystemVaccineResponseDTO>() {
            @Override
            protected SystemVaccineResponseDTO myprocess(SimpleIdRequestDTO requestDTO) throws Exception {
                SystemVaccineResponseDTO responseDTO = new SystemVaccineResponseDTO();
                SystemVaccineModel systemVaccineModel = systemVaccineDao.findId(requestDTO.getId());
                SystemVaccineDTO systemVaccine = new SystemVaccineDTO();
                BeanUtils.copyProperties(systemVaccineModel, systemVaccine);
                responseDTO.setInfo(systemVaccine);
                responseDTO.successfully();
                return responseDTO;
            }
        });
    }

    /**
     * 列表查询
     *
     * @param requestDTO
     * @return
     */
    public SystemVaccineListResponseDTO findList(SystemVaccineRequestDTO requestDTO) {
        return this.service(ServiceActionEnums.SYSTEM_VACCINE_FIND_LIST, requestDTO, SystemVaccineListResponseDTO.class, new MyServiceExecutor<SystemVaccineRequestDTO, SystemVaccineListResponseDTO>() {
            @Override
            protected SystemVaccineListResponseDTO myprocess(SystemVaccineRequestDTO requestDTO) throws Exception {
                SystemVaccineListResponseDTO responseDTO = new SystemVaccineListResponseDTO();
                SystemVaccineModel req = new SystemVaccineModel();
                BeanUtils.copyProperties(requestDTO, req);
                List<SystemVaccineModel> lists = systemVaccineDao.findPage(req);
                List<SystemVaccineDTO> roles = JSONObject.parseArray(JSONArray.toJSONString(lists), SystemVaccineDTO.class);
                responseDTO.setInfos(roles);
                responseDTO.successfully();
                return responseDTO;
            }
        });
    }
}
